﻿
using System;
using Newtonsoft.Json;

namespace SmallShop.Frame
{
    /// <summary>
    /// 扩展方法
    /// </summary>
    public static class ObjectExt
    {
        /// <summary>
        /// 转化为Json字符
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToJson(this object obj)
        {
            return JsonConvert.SerializeObject(obj);
        }

        /// <summary>
        /// 字符发序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="str"></param>
        /// <returns></returns>
        public static T Deserialize<T>(this string str)
        {
            return JsonConvert.DeserializeObject<T>(str);
        }

        /// <summary>
        /// 转化为整型，失败返回0
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static int ToInt(this string str)
        {
            int i;
            int.TryParse(str, out i);
            return i;
        }

        /// <summary>
        /// 转化为长整型，失败返回0
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static long ToLong(this string str)
        {
            long i;
            long.TryParse(str, out i);
            return i;
        }

        /// <summary>
        /// 转化为日期，失败返回 MinValue
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this string str)
        {
            DateTime d;
            DateTime.TryParse(str, out d);
            return d;
        }

        /// <summary>
        /// IsNullOrEmpty 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }

    }
}
